package com.mrx.compose.weather.api.qweather

import com.mrx.compose.weather.api.annotations.Api
import com.mrx.compose.weather.api.qweather.response.BaseResponse
import retrofit2.http.GET
import retrofit2.http.Query
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Api("https://devapi.qweather.com")
interface WeatherAPI {

    @GET("/v7/weather/now")
    suspend fun weatherNow(
        @Query("location") location: String,
        @Query("key") key: String = ApiConfig.key
    ): WeatherResponse

    class WeatherResponse : BaseResponse() {
        var updateTime: String? = null
        var fxLink: String? = null
        var now: Now? = null

        private companion object {
            private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        }

        val updateDate: String
            get() = LocalDateTime.parse(updateTime, DateTimeFormatter.ISO_DATE_TIME).run {
                formatter.format(this)
            }

        data class Now(
            var obsTime: String? = null,
            var temp: Int? = null,
            var feelsLike: Int? = null,
            var icon: Int? = null,
            var text: String? = null,
            var wind360: Int? = null,
            var windDir: String? = null,
            var windScale: Int? = null,
            var windSpeed: Int? = null,
            var humidity: Int? = null,
            var precip: Double? = null,
            var pressure: Int? = null,
            var vis: Int? = null,
            var cloud: Int? = null,
            var dew: Int? = null
        )

        override fun toString(): String {
            return "WeatherResponse(updateTime=$updateTime, fxLink=$fxLink, now=$now, updateDate='$updateDate')"
        }

        fun toWeatherDescriptor(): String {
            val currentNow = now!!
            return """
                当前天气: ${currentNow.text}
                天气温度: ${currentNow.temp}, 体感温度: ${currentNow.feelsLike}
                风向: ${currentNow.windDir}, 风力: ${currentNow.windScale} 级, 风速: ${currentNow.windSpeed}
                更新时间: $updateDate
            """.trimIndent()
        }

    }

}